{extend name="base" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            <select name="channel_id" id="channel_id" lay-filter="channel_id" lay-search="">
                <option value="">选择渠道（全部）</option>
                {volist name="$channel_data" id="vo"}
                <option value="{$vo.cps_id}">{$vo.cps_id}：{$vo.cps_name}</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
        <a href="javascript:;" class="layui-btn my-btn my-btn-sm" style="margin-left:0px;"  id="add">添加</a>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="del">删除</button>

</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use(['table', 'layer'], function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,load = layer.load()
            ,active = {
                reload: function () {
                    var cps_id = $('#channel_id').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: {cps_id:cps_id}
                        ,height: 'full-80'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-80'
            ,url: '{:url("ChannelSon/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
            }
            ,cols: [[
                {field:'cps_id', title: '渠道ID', width: 100}
                ,{field:'cps_name', title: '渠道名称' , width: 200}
                ,{field:'account', title: '账号', width: 200}
                ,{field:'pay_rate', title: '分成比例', width: 150}
                ,{field:'login_time', title: '最后登录时间', width: 200}
                ,{field:'login_ip', title: '最后登录IP', width: 200}
                ,{field:'add_time', title: '添加时间', width: 200}
                ,{field:'operate', title: '操作', align:'center', toolbar:'#tableBar'}
            ]]
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //监听搜索栏
        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['1200px', '700px']
                ,offset: '60px'
                ,resize: true
                ,content: '{:url("ChannelSon/add")}'
            });
        })

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['1200px', '700px']
                    ,content: '{:url("ChannelSon/edit")}?cps_id='+data.cps_id
                    ,cancel: function(){
                        $.post('{:url("ChannelSon/index")}',{cps_id: data.cps_id}, function(json){
                            obj.update({
                                cps_name: json.data[0].cps_name
                                ,pay_rate: json.data[0].pay_rate
                            })
                        }, 'JSON');
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除该渠道吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("ChannelSon/del")}',{cps_id: data.cps_id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:3000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:3000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });
    });
</script>
{/block}